<Project TreatAsLocalProperty="ArchiveTests">
  <!-- We need to set this in order to get extensibility on xunit category traits and other arguments we pass down to xunit via MSBuild properties -->
  <PropertyGroup>
    <IsWasmProject Condition="'$(IsWasmProject)' == ''">true</IsWasmProject>
    <WasmGenerateAppBundle Condition="'$(WasmGenerateAppBundle)' == ''">true</WasmGenerateAppBundle>
    <ArchiveTests Condition="'$(WasmBuildingForNestedPublish)' == 'true'">false</ArchiveTests>
    <BundleTestAppTargets>$(BundleTestAppTargets);BundleTestWasmApp</BundleTestAppTargets>
    <DebuggerSupport Condition="'$(DebuggerSupport)' == '' and '$(Configuration)' == 'Debug'">true</DebuggerSupport>
    <!--
        Some tests depend on debugger attributes, and thus set $(DebuggerSupport)=true to preserve
        them while linking.
        But setting WasmDebugLevel<0 disables optimizations in the interpreter. So setting that
        based on $(DebuggerSupport) has unintended slow down.

        But we do want to set it for Configuration=Debug .
    -->
    <WasmDebugLevel Condition="'$(Configuration)' == 'Debug' and '$(WasmDebugLevel)' == ''">-1</WasmDebugLevel>
    <!-- We want to have WasmDebugLevel=0, but if DebuggerSupport=true then BrowserWasmApp.targets
         *will* override it. So, set this to "reset-to-zero" here, and reset the value later
         in targets. -->
    <WasmDebugLevel Condition="'$(DebuggerSupport)' == 'true' and '$(ContinuousIntegrationBuild)' == 'true' and '$(WasmDebugLevel)' == ''">reset-to-zero</WasmDebugLevel>

    <TrimMode Condition="'$(TrimMode)' == ''">full</TrimMode>
    <JsonSerializerIsReflectionEnabledByDefault Condition="'$(JsonSerializerIsReflectionEnabledByDefault)' == ''">true</JsonSerializerIsReflectionEnabledByDefault>

    <!-- Some tests expect to load satellite assemblies by path, eg. System.Runtime.Loader.Tests,
         so, just setting it true by default -->
    <IncludeSatelliteAssembliesInVFS Condition="'$(IncludeSatelliteAssembliesInVFS)' == ''">true</IncludeSatelliteAssembliesInVFS>

    <!-- Run only if previous command succeeded -->
    <_ShellCommandSeparator Condition="'$(OS)' == 'Windows_NT'">&amp;&amp;</_ShellCommandSeparator>
    <_ShellCommandSeparator Condition="'$(OS)' != 'Windows_NT'">&amp;&amp;</_ShellCommandSeparator>
    <XUnitUseRandomizedTestOrderer Condition="'$(XUnitUseRandomizedTestOrderer)' == '' and '$(IsTestProject)' == 'true'">true</XUnitUseRandomizedTestOrderer>
    <WasmXHarnessTestsTimeout Condition="'$(WasmXHarnessTestsTimeout)' == ''">00:30:00</WasmXHarnessTestsTimeout>
    <RunWorkingDirectory>$(BundleDir)</RunWorkingDirectory>

    <WasmCommonTargetsPath Condition="Exists('$(MSBuildThisFileDirectory)WasmApp.Common.props')">$(MSBuildThisFileDirectory)</WasmCommonTargetsPath>
    <WasmCommonTargetsPath Condition="'$(WasmCommonTargetsPath)' == ''">$([MSBuild]::NormalizeDirectory($(MonoProjectRoot), 'wasm', 'build'))</WasmCommonTargetsPath>
  </PropertyGroup>

  <PropertyGroup Condition="'$(EnableAggressiveTrimming)' == 'true'">
    <!-- suppress warnings as these are tests, and not expected to be trim-safe -->
    <SuppressTrimAnalysisWarnings>true</SuppressTrimAnalysisWarnings>
    <!-- This warning code isn't yet included in SuppressTrimAnalysisWarnings -->
    <NoWarn>$(NoWarn);IL2118</NoWarn>
    <!-- IL2121: Unnecessary UnconditionalSuppressMessage attribute -->
    <NoWarn>$(NoWarn);IL2121</NoWarn>
  </PropertyGroup>

  <PropertyGroup>
    <BuildAOTTestsOn Condition="'$(ContinuousIntegrationBuild)' == 'true' and '$(Scenario)' == 'BuildWasmApps'">helix</BuildAOTTestsOn>
    <BuildAOTTestsOn Condition="'$(BuildAOTTestsOnHelix)' == 'true'">helix</BuildAOTTestsOn>
    <BuildAOTTestsOn Condition="'$(BuildAOTTestsOn)' == ''">local</BuildAOTTestsOn>
  </PropertyGroup>

  <PropertyGroup Condition="'$(BuildAOTTestsOn)' == 'local'">
    <!--
      When building for BuildAOTTestsOnHelix=true, the BrowserWasmApp.targets are *not* imported, because
      they get instead used by the AOT proxy project on helix.

      On the build machine only the regular part of the build is run, which includes trimming. But if
      BrowserWasmApp.targets modify any trimming arguments, then those will not get picked up by this build.
      For example - linker substitution files used with simd builds.

      So, set those parameters explicitly here.
      -->
    <_ExtraTrimmerArgs Condition="'$(WasmEnableSIMD)' == 'true' and '$(RunAOTCompilation)' == 'true'">$(_ExtraTrimmerArgs) --substitutions "$(BrowserProjectRoot)build\ILLink.Substitutions.WasmIntrinsics.xml"</_ExtraTrimmerArgs>
    <_ExtraTrimmerArgs Condition="'$(WasmEnableSIMD)' != 'true'">$(_ExtraTrimmerArgs) --substitutions "$(BrowserProjectRoot)build\ILLink.Substitutions.NoWasmIntrinsics.xml"</_ExtraTrimmerArgs>
  </PropertyGroup>

  <ItemGroup>
    <_AOT_InternalForceInterpretAssemblies Include="@(HighAotMemoryUsageAssembly)" />
  </ItemGroup>

  <PropertyGroup>
    <!-- non-library tests have IsWasmProject==false -->
    <BundleTestWasmAppDependsOn Condition="'$(BuildAOTTestsOn)' == 'local'">WasmTriggerPublishApp</BundleTestWasmAppDependsOn>
    <BundleTestWasmAppDependsOn Condition="'$(BuildAOTTestsOnHelix)' == 'true'">$(BundleTestWasmAppDependsOn);_BundleAOTTestWasmAppForHelix</BundleTestWasmAppDependsOn>

    <!-- Use BundleDir here, since WasmAppDir is set in a target, and `dotnet run` reads
         $(Run*) without running any targets -->
    <_RuntimeConfigJsonPath>$([MSBuild]::NormalizePath($(BundleDir), 'WasmTestRunner.runtimeconfig.json'))</_RuntimeConfigJsonPath>
    <RunArguments>exec &quot;$([MSBuild]::NormalizePath($(WasmAppHostDir), 'WasmAppHost.dll'))&quot; --runtime-config &quot;$(_RuntimeConfigJsonPath)&quot; $(WasmHostArguments) $(StartArguments) $(WasmXHarnessMonoArgs) $(_AppArgs)</RunArguments>
  </PropertyGroup>

  <PropertyGroup Condition="'$(BuildAOTTestsOnHelix)' == 'true'">
    <!-- wasm targets are not imported at all, in this case, because we run the wasm build on helix -->
  </PropertyGroup>

  <ItemGroup>
    <WasmExtraFilesToDeploy Condition="'$(_UseWasmSymbolicator)' == 'true'" Include="$(MonoProjectRoot)wasm\data\wasm-symbol-patterns.txt" />
    <WasmExtraFilesToDeploy Condition="'$(_UseWasmSymbolicator)' == 'true'" Include="$(ArtifactsBinDir)WasmSymbolicator\$(Configuration)\$(NetCoreAppToolCurrent)\WasmSymbolicator.dll" />
  </ItemGroup>

  <Target Name="BundleTestWasmApp" DependsOnTargets="$(BundleTestWasmAppDependsOn)" />

  <UsingTask Condition="'$(BuildAOTTestsOnHelix)' == 'true'"
             TaskName="Microsoft.WebAssembly.Build.Tasks.GenerateAOTProps"
             AssemblyFile="$(WasmBuildTasksAssemblyPath)" />

  <Target Name="_BundleAOTTestWasmAppForHelix" DependsOnTargets="$(_BundleAOTTestWasmAppForHelixDependsOn)">
    <PropertyGroup>
      <_MainAssemblyPath Condition="'%(WasmAssembliesToBundle.FileName)' == $(AssemblyName) and '%(WasmAssembliesToBundle.Extension)' == '.dll'">%(WasmAssembliesToBundle.Identity)</_MainAssemblyPath>
      <RuntimeConfigFilePath>$([System.IO.Path]::ChangeExtension($(_MainAssemblyPath), '.runtimeconfig.json'))</RuntimeConfigFilePath>
    </PropertyGroup>

    <Error Text="Item WasmAssembliesToBundle is empty. This is likely an authoring error." Condition="@(WasmAssembliesToBundle->Count()) == 0" />

    <ItemGroup>
      <BundleFiles Include="@(WasmAssembliesToBundle)"          TargetDir="publish\%(WasmAssembliesToBundle.RecursiveDir)" />
      <BundleFiles Include="$(RuntimeConfigFilePath)"           TargetDir="publish" />

      <BundleFiles Include="$(WasmProjectRoot)data\aot-tests\*" TargetDir="publish" />
      <!-- FIXME: what would be the correct place to do this? -->
      <BundleFiles Include="$(WasmProjectRoot)build\WasmApp.Common*"  TargetDir="publish" />
    </ItemGroup>

    <ItemGroup Condition="'$(DebuggerSupport)' == 'true'">
      <!-- Add any pdb files, if available -->
      <_BundlePdbFiles Include="$([System.IO.Path]::ChangeExtension('%(WasmAssembliesToBundle.Identity)', '.pdb'))" />
      <BundleFiles Include="@(_BundlePdbFiles)" TargetDir="publish" Condition="Exists(%(_BundlePdbFiles.Identity))" />
    </ItemGroup>

    <!-- To recreate the original project on helix, we need to set the wasm properties also, same as the
         library test project. Eg. $(InvariantGlobalization) -->
    <ItemGroup>
      <_WasmPropertyNames Include="AOTMode" />
      <_WasmPropertyNames Include="AssemblyName" />
      <_WasmPropertyNames Include="DisableParallelAot" />
      <_WasmPropertyNames Include="IncludeSatelliteAssembliesInVFS" />
      <_WasmPropertyNames Include="InvariantGlobalization" />
      <_WasmPropertyNames Include="WasmBuildNative" />
      <_WasmPropertyNames Include="WasmDebugLevel" />
      <_WasmPropertyNames Include="WasmDedup" />
      <_WasmPropertyNames Include="WasmLinkIcalls" />
      <_WasmPropertyNames Include="WasmNativeStrip" />
      <_WasmPropertyNames Include="_WasmDevel" />
      <_WasmPropertyNames Include="_WasmStrictVersionMatch" />
      <_WasmPropertyNames Include="WasmEmitSymbolMap" />

      <_WasmPropertiesToPass
        Include="$(%(_WasmPropertyNames.Identity))"
        Name="%(_WasmPropertyNames.Identity)"
        ConditionToUse__="%(_WasmPropertyNames.ConditionToUse__)" />

      <_WasmVFSFilesToCopy Include="@(WasmFilesToIncludeInFileSystem)" />
      <_WasmVFSFilesToCopy TargetPath="%(FileName)%(Extension)" Condition="'%(_WasmVFSFilesToCopy.TargetPath)' == ''" />

      <_WasmExtraFilesToCopy Include="@(WasmExtraFilesToDeploy)" />
      <_WasmExtraFilesToCopy TargetPath="%(FileName)%(Extension)" Condition="'%(_WasmExtraFilesToCopy.TargetPath)' == ''" />

      <!-- Example of passing items to the project

          <_WasmItemsToPass Include="@(BundleFiles)" OriginalItemName__="BundleFiles" ConditionToUse__="'$(Foo)' != 'true'" />

      -->

      <_WasmItemsToPass Include="@(_AOT_InternalForceInterpretAssemblies)" OriginalItemName__="_AOT_InternalForceInterpretAssemblies" />

    </ItemGroup>

    <!-- This file gets imported by the project file on helix -->
    <GenerateAOTProps
        Properties="@(_WasmPropertiesToPass)"
        Items="@(_WasmItemsToPass)"
        OutputFile="$(BundleDir)publish\ProxyProjectForAOTOnHelix.props" />

    <Copy SourceFiles="@(BundleFiles)"         DestinationFolder="$(BundleDir)%(TargetDir)" />
    <Copy SourceFiles="@(_WasmVFSFilesToCopy)" DestinationFiles="$(BundleDir)\vfsFiles\%(_WasmVFSFilesToCopy.TargetPath)" />
    <Copy SourceFiles="@(_WasmExtraFilesToCopy)" DestinationFiles="$(BundleDir)\extraFiles\%(_WasmExtraFilesToCopy.TargetPath)" />
  </Target>

  <!-- linker automatically picks up the .pdb files, but they are not added to the publish list.
       Add them explicitly here, so they can be used with WasmAppBuilder -->
  <Target Name="AddPdbFilesToPublishList" AfterTargets="ILLink" Condition="'$(DebuggerSupport)' == 'true'">
    <ItemGroup>
      <_PdbFilesToCheck Include="$([System.IO.Path]::ChangeExtension('%(ResolvedFileToPublish.Identity)', '.pdb'))"
                        Condition="'%(ResolvedFileToPublish.Extension)' == '.dll'" />

      <ResolvedFileToPublish Include="@(_PdbFilesToCheck)"
                             Condition="Exists(%(_PdbFilesToCheck.Identity))"
                             RelativePath="%(_PdbFilesToCheck.FileName)%(_PdbFilesToCheck.Extension)" />
    </ItemGroup>
  </Target>

  <Target Name="DeployHelixTargetsFile" AfterTargets="ArchiveTests" Condition="'$(HelixTargetsFile)' != ''">
    <Copy SourceFiles="$(HelixTargetsFile)" DestinationFiles="$(TestArchiveTestsDir)$(TestProjectName).helix.targets" SkipUnchangedFiles="true" />
  </Target>
</Project>
